﻿package mortal.game.view.chat.chatTrumpet
{
    import Message.Public.*;
    import com.gengine.keyBoard.*;
    import com.mui.controls.*;
    import com.mui.utils.*;
    import extend.language.*;
    import flash.events.*;
    import flash.text.*;
    import mortal.common.*;
    import mortal.common.font.*;
    import mortal.common.global.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.manager.msgTip.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.utils.*;
    import mortal.game.view.chat.*;
    import mortal.game.view.chat.selectPanel.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.button.*;
    import mortal.game.view.common.item.*;
    import mortal.game.view.shopMall.data.*;
    import mortal.game.view.shopMall.view.*;
    import mortal.game.view.vip.panel.*;
    import mortal.game.view.wizard.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class ChatTrumpetSendWindow extends SmallWindow
    {
        private const itemCode:int = 180040000;
        private const itemCodeCross:int = 180040001;
        protected var _tabBar:GTabarNew;
        protected var _contentPanel:GSprite;
        protected var _nameText:GTextFiled;
        protected var _shopItem:ShopSellItem;
        protected var _leftTimeText:GTextFiled;
        protected var _leftFreeTime:GTextFiled;
        protected var _vipAddTips:VipOpenTipsPanel;
        protected var _crossNOVIP:GImageBitmap;
        protected var _tiSend:GTextInput;
        protected var _cbClose:GCheckBox;
        protected var _btnFace:GLoadingButton;
        protected var _btnColor:GLoadingButton;
        protected var _btnSend:TimeButton;
        protected var _moneyItem:MoneyItem;
        protected var _numericStepper:GNumericStepper;
        protected var _color:int;
        protected var _vipFreeTime:int;
        private var sendTotal:int = 0;
        private static var _instance:ChatTrumpetSendWindow;

        public function ChatTrumpetSendWindow(param1:ILayer = null)
        {
            super(param1);
            setSize(276, 307);
            title = Language.getString(11525);
            return;
        }// end function

        override protected function configParams() : void
        {
            paddingBottom = 36;
            paddingLeft = 0;
            paddingRight = 0;
            blurTop = 5;
            blurBottom = 4;
            blurLeft = 8;
            blurRight = 8;
            _contentX = blurLeft;
            _contentY = blurTop;
            return;
        }// end function

        override protected function addWindowLine() : void
        {
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            titleHeight = 50;
            super.createDisposedChildrenImpl();
            this._contentPanel = UIFactory.getUICompoment(GSprite, 0, 16, this);
            this._tabBar = UIFactory.gTabBarNew(2, 32, [{label:Language.getString(11526), name:"trumpet"}, {label:Language.getString(21360), name:"serverTrumpet"}], 284, 307, 63, 22, this, this.onTabarChange);
            pushUIToDisposeVec(this._tabBar);
            pushUIToDisposeVec(UIFactory.bg(14, 46, 264, 136, this._contentPanel, ImagesConst.ChatTrumpetPanelTop));
            this._nameText = UIFactory.gTextField("", 35, 45, 80, 20, this._contentPanel);
            pushUIToDisposeVec(this._nameText);
            pushUIToDisposeVec(UIFactory.bg(29, 66, 237, 2, this._contentPanel, "SplitLine"));
            this._shopItem = UICompomentPool.getUICompoment(ShopSellItem);
            this._shopItem.x = 27;
            this._shopItem.y = 71;
            this._shopItem.setItemStyle(ItemStyleConst.Big, ImagesConst.ShopItemBg, 6, 6);
            pushUIToDisposeVec(this._shopItem);
            this._contentPanel.addChild(this._shopItem);
            this._leftTimeText = UIFactory.gTextField(Language.getString(11527), 107, 75, 89, 20, this._contentPanel);
            this._leftFreeTime = UIFactory.gTextField(Language.getStringByParam(11528, "0"), 195, 75, 65, 20, this._contentPanel, GlobalStyle.textFormatAnjin);
            pushUIToDisposeVec(this._leftTimeText);
            pushUIToDisposeVec(this._leftFreeTime);
            pushUIToDisposeVec(UIFactory.gTextField(Language.getString(11529), 107, 97, 65, 20, this._contentPanel));
            pushUIToDisposeVec(UIFactory.gTextField("", 175, 97, 65, 20, this._contentPanel, GlobalStyle.textFormatAnjin));
            this._moneyItem = UICompomentPool.getUICompoment(MoneyItem);
            UIFactory.setObjAttri(this._moneyItem, 200, 95, -1, -1, this._contentPanel);
            pushUIToDisposeVec(this._moneyItem);
            this._numericStepper = UIFactory.gNumericStepper(106, 120, 50, 20, this._contentPanel, 99, 1, "NumericStepper", GlobalStyle.textFormatBai);
            this._numericStepper.value = 1;
            pushUIToDisposeVec(this._numericStepper);
            var _loc_1:* = UIFactory.gLoadingButton(ResFileConst.ShopBuy, 180, 119, 49, 23, this._contentPanel);
            _loc_1.configEventListener(MouseEvent.CLICK, this.onClickBuyBtn);
            pushUIToDisposeVec(_loc_1);
            this._vipAddTips = UICompomentPool.getUICompoment(VipOpenTipsPanel);
            this._vipAddTips.move(26, 146);
            var _loc_2:int = 32;
            this._vipAddTips.resetLevelTextPos(55, 9, 66, 9);
            this._vipAddTips.resetValueTextPos(160, 10, 190, 9);
            this._vipAddTips.bgUrl = ImagesConst.vipOpenTipsBg_mid;
            this._vipAddTips.valueType = VipOpenTipsPanel.numberValue;
            this._vipAddTips.isShowLeftNum(true);
            this._vipAddTips.vipType = EVipRecordType._EVipRecordTypeTrumpet;
            this._contentPanel.addChild(this._vipAddTips);
            pushUIToDisposeVec(this._vipAddTips);
            this._crossNOVIP = UIFactory.gImageBitmap(ImagesConst.CrossTrumptNOVIP, 30, 167, this);
            this._tiSend = UIFactory.gTextInput(13, 185, 265, 70, this._contentPanel);
            this._tiSend.maxChars = 50;
            this._tiSend.configEventListener(KeyboardEvent.KEY_DOWN, this.onKeySend);
            pushUIToDisposeVec(this._tiSend);
            this._cbClose = UIFactory.checkBox(Language.getString(11530), 16, 267, 120, 28, this._contentPanel);
            pushUIToDisposeVec(this._cbClose);
            this._btnFace = UIFactory.gLoadingButton(ResFileConst.ChatTrumpetFaceBtn, 146, 268, 24, 24, this._contentPanel);
            FacePanel.registBtn(this._btnFace, this.selectFace);
            pushUIToDisposeVec(this._btnFace);
            this._btnColor = UIFactory.gLoadingButton(ResFileConst.ChatTrumpetColorBtn, 170, 268, 24, 24, this._contentPanel);
            ColorSelector.registBtn(this._btnColor, this.selectColor);
            pushUIToDisposeVec(this._btnColor);
            this._btnSend = UIFactory.timeButton(Language.getString(11531), 198, 269, 60, 22, this._contentPanel, Cache.instance.cd.CHAT);
            this._btnSend.cdTime = ChatUtil.chatCd;
            this._btnSend.configEventListener(MouseEvent.CLICK, this.onClickBtnSend);
            pushUIToDisposeVec(this._btnSend);
            this.updateVIPFreeTime();
            this.onTabarChange();
            NetDispatcher.addCmdListener(ServerCommand.VipFreeInfoUpdate, this.updateVIPFreeTime);
            return;
        }// end function

        public function changeToCrossTab() : void
        {
            if (Cache.instance.role.entityInfo.VIP < 4)
            {
                MsgManager.showRollTipsMsg(Language.getString(11532));
                return;
            }
            this._tabBar.selectedIndex = 1;
            this.onTabarChange();
            return;
        }// end function

        private function isVIPCrossTrumpet() : Boolean
        {
            return Cache.instance.vip.getTVipConfigByType(EVipRecordType._EVipRecordTypeCrossTrumpet) != "0";
        }// end function

        protected function onTabarChange(event:Event = null) : void
        {
            var _loc_2:int = 0;
            switch(this._tabBar.selectedIndex)
            {
                case 0:
                {
                    _loc_2 = this.itemCode;
                    this._leftTimeText.visible = true;
                    this._leftFreeTime.visible = true;
                    this._crossNOVIP.visible = false;
                    this._vipAddTips.visible = true;
                    break;
                }
                case 1:
                {
                    if (Cache.instance.role.entityInfo.VIP < 4)
                    {
                        MsgManager.showRollTipsMsg(Language.getString(11532));
                        this._tabBar.selectedIndex = 0;
                        this.onTabarChange();
                        return;
                    }
                    _loc_2 = this.itemCodeCross;
                    this._leftTimeText.visible = false;
                    this._leftFreeTime.visible = false;
                    this._vipAddTips.visible = false;
                    this._crossNOVIP.visible = true;
                    break;
                }
                default:
                {
                    break;
                }
            }
            var _loc_3:* = new ItemData(_loc_2);
            this._nameText.htmlText = ItemsUtil.getItemName(_loc_3);
            var _loc_4:* = ShopConfig.instance.getFastBuyItemByCode(_loc_2);
            this._moneyItem.update(_loc_4.tShopSell.offer, ShopConfig.instance.getTShopByCode(_loc_4.tShopSell.shopCode).unit);
            this._shopItem.shopItemData = ShopConfig.instance.getFastBuyItemByCode(_loc_3.itemCode);
            return;
        }// end function

        protected function updateVIPFreeTime(param1 = null) : void
        {
            this._vipFreeTime = Cache.instance.vip.getLeftFreeTimesByType(EVipRecordType._EVipRecordTypeTrumpet);
            this._leftFreeTime.text = Language.getStringByParam(11533, this._vipFreeTime.toString());
            return;
        }// end function

        private function clickHandler(event:MouseEvent) : void
        {
            this._numericStepper.value = 99;
            return;
        }// end function

        protected function onClickBtnSend(event:MouseEvent) : void
        {
            this.send();
            return;
        }// end function

        protected function onKeySend(event:KeyboardEvent) : void
        {
            if (event.keyCode == KeyCode.ENTER)
            {
                this.send();
            }
            return;
        }// end function

        protected function send() : void
        {
            var _loc_2:Object = null;
            var _loc_1:* = this._tiSend.text;
            if (Cache.instance.role.entityInfo.level < 50)
            {
                MsgManager.showRollTipsMsg(Language.getString(11534));
                return;
            }
            if (Cache.instance.vip.level < 1)
            {
                MsgManager.showRollTipsMsg(Language.getString(21356));
                return;
            }
            if (Cache.instance.vip.level == 1 && this.sendTotal >= 3)
            {
                MsgManager.showRollTipsMsg(Language.getString(21357));
                return;
            }
            if (!_loc_1)
            {
                MsgManager.showRollTipsMsg(Language.getString(21358));
            }
            else if (this._tabBar.selectedIndex == 0 && this._vipFreeTime == 0 && Cache.instance.pack.backPackCache.getItemCountByItemData(new ItemData(this.itemCode)) <= 0 || this._tabBar.selectedIndex == 1 && Cache.instance.pack.backPackCache.getItemCountByItemData(new ItemData(this.itemCodeCross)) <= 0)
            {
                MsgManager.showMouseMsg(Language.getString(21359), MsgRollTipsType.msgRollTips3);
            }
            else
            {
                _loc_2 = new Object();
                if (this._tabBar.selectedIndex == 0)
                {
                    _loc_2.area = ChatArea.Speaker;
                }
                else
                {
                    _loc_2.area = ChatArea.CrossSpeaker;
                }
                _loc_2.content = _loc_1;
                _loc_2.color = this._color;
                Dispatcher.dispatchEvent(new DataEvent(EventName.ChatSend, _loc_2));
                var _loc_3:String = this;
                var _loc_4:* = this.sendTotal + 1;
                _loc_3.sendTotal = _loc_4;
                if (this._cbClose.selected)
                {
                    this.hide();
                }
            }
            return;
        }// end function

        protected function selectFace(param1) : void
        {
            this._tiSend.appendText("/" + param1.toString() + " ");
            this._tiSend.setFocus();
            this._tiSend.setSelection(this._tiSend.text.length, this._tiSend.text.length);
            return;
        }// end function

        protected function selectColor(param1:int) : void
        {
            this._color = param1;
            this.setInputTextFormat();
            return;
        }// end function

        private function setInputTextFormat() : void
        {
            var _loc_1:* = new GTextFormat(FontUtil.defaultName, 12, this._color);
            _loc_1.leading = 3;
            this._tiSend.setStyle("textFormat", _loc_1);
            this._tiSend.setStyle("textPadding", 5);
            return;
        }// end function

        protected function onClickBuyBtn(event:MouseEvent) : void
        {
            this._shopItem.shopItemData.num = int(this._numericStepper.value);
            Dispatcher.dispatchEvent(new DataEvent(EventName.BuyItem, this._shopItem));
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            NetDispatcher.removeCmdListener(ServerCommand.VipFreeInfoUpdate, this.updateVIPFreeTime);
            this._vipFreeTime = 0;
            this._leftFreeTime = null;
            this._shopItem = null;
            this._tiSend = null;
            this._cbClose = null;
            this._btnFace = null;
            this._btnColor = null;
            this._btnSend = null;
            this._numericStepper = null;
            this._contentPanel.dispose();
            this._contentPanel = null;
            this._tabBar = null;
            return;
        }// end function

        public static function get instance() : ChatTrumpetSendWindow
        {
            if (!_instance)
            {
                _instance = new ChatTrumpetSendWindow;
            }
            return _instance;
        }// end function

    }
}
